package com.maiji.cloud.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

public class SMS {

    public static JSONObject sendSms (String tel, Template template) {
//        DefaultProfile profile = DefaultProfile.getProfile("default",
//                "LTAI1SlxSZGC4Etk", "6kOb747zTRWx7MtqhbeqgqQkbvKhmq");
        DefaultProfile profile = DefaultProfile.getProfile("default",
                "LTAI4G5aCWqVpP8mHCkaEUW2", "iO9h2GGONstrCkK1PIju4angOFlImh");
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("PhoneNumbers", tel);
//        request.putQueryParameter("SignName", "麦吉");
        request.putQueryParameter("SignName", "中广视媒");
        request.putQueryParameter("TemplateCode", "SMS_193105403");
//        request.putQueryParameter("TemplateCode", template.getCode());
        int randomCode = randomCode();
        request.putQueryParameter("TemplateParam", "{\"code\":"+randomCode+"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            String data = response.getData().replace("{", "{\"code\":"+randomCode+",");
            return JSON.parseObject(data);
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void SendBatchSms (String[] tels, Template template) {
        DefaultProfile profile = DefaultProfile.getProfile("default", "LTAI1SlxSZGC4Etk", "6kOb747zTRWx7MtqhbeqgqQkbvKhmq");
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendBatchSms");
        request.putQueryParameter("PhoneNumberJson", JSON.toJSONString(tels));
        request.putQueryParameter("TemplateCode", template.getCode());
        String[] signNames = new String[tels.length];
        Integer[] templateCodes = new Integer[tels.length];
        for (int i=0; i<tels.length; i++) {
            signNames[i] = "麦吉";
            templateCodes[i] = randomCode();
        }
        request.putQueryParameter("SignNameJson", JSON.toJSONString(signNames));
        request.putQueryParameter("TemplateParamJson", JSON.toJSONString(templateCodes));
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

    public static int randomCode() {
        return (int) ((Math.random() * 9 + 1) * 100000);
    }

    public static void main (String[] args) {
        sendSms ("15053766744", Template.LOGINCONFIRM);
    }

    public enum Template {
        UPDATEPWD ("修密码改", "SMS_166910718"),  // 1
        USERLOGON ("用户注册", "SMS_166910719"),  // 2
        LOGINCONFIRM ("登录确认", "SMS_166910721");  //3

        private String message;
        private String code;

        private Template (String message, String code) {
            this.message = message;
            this.code = code;
        }

        public String getmessage() {
            return message;
        }

        public void setmessage(String message) {
            this.message = message;
        }

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

        @Override
        public String toString() {
            return this.code+"_"+this.message;
        }

    }

}

